﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Principal;
using Matrix_network.Code;

namespace Matrix_network
{
    public partial class MyProfile : Page
    {
        protected users ViewingUser;
        protected bool IsCurrentUser;

        protected MatrixDataClassesDataContext DataContext = new MatrixDataClassesDataContext();

        protected int UserId;
        protected int CurrentUserId;

        protected bool RedirectRequired;

        protected bool IsCurrentUserFriend;

        protected void Page_Load(object sender, EventArgs e)
        {
            UserId = CustomMethods.UserIdFromUrl();
            CurrentUserId = User.Identity.UserId();

            if (UserId == CustomMethods.IncorrectUserId)
            {
                Response.Redirect("/");
                return;
            }

            IsCurrentUser = UserId == CurrentUserId;

            ViewingUser = DataContext.users.First(item => item.id == UserId);

            if (string.IsNullOrEmpty(fname.Text))
                fname.Text = ViewingUser.name;
            if (string.IsNullOrEmpty(lname.Text))
                lname.Text = ViewingUser.surname;

            IsCurrentUserFriend = DataContext.friends.Count(item => item.usr_id == CurrentUserId && item.friend_id == UserId) > 0;
        }

        protected void UploadPhoto_Click(object sender, EventArgs e)
        {
            var data = new System.Data.Linq.Binary(photo.FileBytes);
            var result = DataContext.add_photo(CurrentUserId, data);
            ViewingUser.ava_id = (int)result.First().Column1;
            DataContext.SubmitChanges();
        }

        protected void EditUsername_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                ViewingUser.name = fname.Text;
                ViewingUser.surname = lname.Text;
                DataContext.SubmitChanges();
            }
        }

        protected void NewProperty_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                ViewingUser.about_person.Add(new about_person { usr_id = CurrentUserId, name = propertyName.Text, val = propertyValue.Text });
                DataContext.SubmitChanges();

                RedirectRequired = true;
            }
        }

        protected void CheckUniquePropertyName(object source, ServerValidateEventArgs args)
        {
            args.IsValid = ViewingUser.about_person.Count(item => item.name == propertyName.Text) == 0;
        }

        protected void AddFriend_Click(object sender, EventArgs e)
        {
            DataContext.add_friend(CurrentUserId, UserId);
            DataContext.SubmitChanges();

            IsCurrentUserFriend = true;
        }

        protected void RemoveFriend_Click(object sender, EventArgs e)
        {
            DataContext.delete_friend(CurrentUserId, UserId);
            DataContext.SubmitChanges();

            IsCurrentUserFriend = false;
        }
    }

    public class AboutUserDataSource
    {
        private MatrixDataClassesDataContext _dataContext = new MatrixDataClassesDataContext();
        private int _userId = CustomMethods.UserIdFromUrl();

        public about_person[] Select()
        {
            return _dataContext.about_person.Where(item => item.usr_id == _userId).ToArray();
        }

        public void Update(int id, string name, string val)
        {
            var aboutUser = _dataContext.about_person.First(item => item.id == id);
            aboutUser.name = name;
            aboutUser.val = val;

            _dataContext.SubmitChanges();
        }

        public void Delete(int id)
        {
            var aboutUser = _dataContext.about_person.First(item => item.id == id);
            _dataContext.about_person.DeleteOnSubmit(aboutUser);

            _dataContext.SubmitChanges();
        }
    }

    public static class CustomMethods
    {
        public const int IncorrectUserId = -1;

        public static int UserIdFromUrl()
        {
            var context = HttpContext.Current;
            var userIdentity = context.User.Identity;

            int userId;
            return int.TryParse(context.Request.QueryString["id"], out userId) ? userId : userIdentity.UserId();
        }

        public static int UserId(this IIdentity identity)
        {
            int userId;
            return identity.IsAuthenticated && int.TryParse(identity.Name, out userId) ? userId : IncorrectUserId;
        }
    }
}